/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scrumpmt.web;

import java.util.logging.Level;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.scrumpmt.domain.Colaborador;
import org.scrumpmt.ejb.ColaboradorFacade;

@ManagedBean(name = "login")
@SessionScoped
/**
 *
 * @author Cristian
 */
public class login extends BasicBean {

    private ColaboradorFacade colaboradorFacade = lookupColaboradorFacade();
    private String email;
    private String psswd;
    private int error;
    private String nombre;
    private Boolean administrador;

    public Boolean getAdministrador() {
        return administrador;
    }

    public void setAdministrador(Boolean administrador) {
        this.administrador = administrador;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    /**
     * Get the value of psswd
     *
     * @return the value of psswd
     */
    public String getPsswd() {
        return psswd;
    }

    /**
     * Set the value of psswd
     *
     * @param psswd new value of psswd
     */
    public void setPsswd(String psswd) {
        this.psswd = psswd;
    }

    /**
     * Get the value of email
     *
     * @return the value of email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Set the value of email
     *
     * @param email new value of email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    public String submit() {
        error = 0;
        if (datosValidos()) {
            Colaborador colaborador = colaboradorFacade.findByEmail(email);
            if (colaborador != null) {
                String pass = colaborador.getPassword();
                if (pass.equals(this.psswd)) {
                    this.administrador = colaborador.getAdministrador();
                    this.nombre = colaborador.getNombre();
                    return "/producto/listado";
                }
            }
        }
        error = 1;
        return "";

    }

    public int getError() {
        return error;
    }

    public void setError(int error) {
        this.error = error;
    }

    private boolean datosValidos() {
        if (email != null
                && psswd != null) {
            return true;
        } else {
            return false;
        }
    }

    public String logOut() {
        this.administrador = null;
        this.email = null;
        this.nombre = null;
        this.psswd = null;
        error = 0;
        return "index";
    }

    /**
     * Creates a new instance of login
     */
    public login() {
        error = 0;
    }
    
    private ColaboradorFacade lookupColaboradorFacade() {
        try {
            Context c = new InitialContext();
            return (ColaboradorFacade) c.lookup("java:global/ScrumPMT/ScrumPMT-ejb/ColaboradorFacade!org.scrumpmt.ejb.ColaboradorFacade");
        } catch (NamingException ne) {
            log.log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
